package view.component
{
	import enums.main.UISize;
	
	import feathers.controls.Label;
	import feathers.controls.List;
	import feathers.controls.Screen;
	import feathers.controls.Scroller;
	import feathers.controls.renderers.DefaultListItemRenderer;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.data.ListCollection;
	import feathers.layout.VerticalLayout;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Sprite;
	
	import view.render.BaseItem;
	
	
	/**
	 * 列表界面基类
	 * @author £iang
	 * 
	 */	
	public class BaseListView extends Screen implements IAnimateNaviView
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 标题组 */
		public var titleGroup:Sprite;
		/** 列表 */
		public var list:List;
		/** 第一标题 */
		public var firstTitle:Label;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 列表数据 */
		protected var _listData:ListCollection;
		/**  列表高度 */
		protected var _listH:Number;
		/** item高度 */
		protected var _itemH:Number;
		/** 页面最多item数 */
		protected var _maxItemCount:int;
		/** 第一标题名 */
		protected var _firstName:String;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{ 
			_maxItemCount = Math.ceil( _listH / _itemH ) + 1;
			
			titleGroup = new Sprite();
			this.addChild( titleGroup );
			titleGroup.alpha = 0;
			
			firstTitle = new Label();
			firstTitle.text = _firstName;
			firstTitle.textRendererProperties.wordWrap = true;
			firstTitle.textRendererProperties.isHTML = true;
			titleGroup.addChild( firstTitle );
			
			initializeOther();
			
			list = new List();
			this.addChild( list );
			
			list.width = UISize.MAIN_W;
			list.height = _listH;
			list.itemRendererProperties.accessoryField  = "item";
			list.itemRendererFactory = itemRenderFactroy;
			list.scrollerProperties.scrollBarDisplayMode = Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			( list.layout as VerticalLayout ).gap = 3 * this.dpiScale;
			
			addListener();
		}
		
		protected function initializeOther():void
		{
			
		}
		
		/**
		 * itemRenderer皮肤 
		 * @return renderer
		 * 
		 */
		private function itemRenderFactroy():IListItemRenderer
		{
			var renderer:DefaultListItemRenderer = new DefaultListItemRenderer();
			renderer.accessoryGap = 0;
			renderer.gap = 0;
			renderer.accessoryPosition = "left";
			renderer.width = UISize.MAIN_W * this.dpiScale;
			renderer.height = _itemH;
			renderer.itemHasIcon = false;
			renderer.itemHasLabel = false;
			return renderer;
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		protected function addListener():void
		{
			
		}
		
		/**
		 * 滚动条滚动至某一item 
		 * @param itemIndex item下标
		 * 
		 */		
		public function showItem( itemIndex:int ):void
		{
			var len:int = _listData.length;
			var vPos:Number = 0;
			
			list.stopScrolling();
			if ( itemIndex + _maxItemCount - 1 > len )
				vPos =  list.maxVerticalScrollPosition;
			else
				vPos = itemIndex * ( _itemH + 3 * this.dpiScale ) ;
			list.verticalScrollPosition = vPos;
			list.selectedIndex = itemIndex;
		}
		
		/**
		 * 播放显示动画
		 * 
		 */	
		public function playShowAnimate():void
		{
			var tween:Tween = new Tween( titleGroup, 0.4, "linear" );
			var len:int = _listData.length;
			
			tween.animate( "alpha", 1 );
			Starling.juggler.add( tween );
			
			var endIndex:int = _maxItemCount + 1;
			
			if ( endIndex > len )
				endIndex = len;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				if ( i >= 0 && i < endIndex )
					( _listData.data[ i ].item as BaseItem ).playShowAnimate( i );
				else
					( _listData.data[ i ].item as BaseItem ).skipShowAnimate();
			}
		}
		
		/**
		 * 播放隐藏动画 
		 * 
		 */	
		public function playHideAnimate():void
		{
			var tween:Tween = new Tween( titleGroup, 0.4, "linear" );
			var len:int = _listData.length;
			
			tween.animate( "alpha", 0 );
			Starling.juggler.add( tween );
			
			var startIndex:int = int( list.verticalScrollPosition / _itemH ) - 1;
			var maxIndex:int = len - _maxItemCount;
			var endIndex:int;
			
			if ( startIndex < 0 )
				startIndex = 0;
			
			if ( maxIndex < 0 )
				maxIndex = 0;
			
			if ( startIndex >= maxIndex )
				startIndex = maxIndex;
			
			endIndex = startIndex + _maxItemCount + 1;
			
			if ( endIndex > len )
				endIndex = len;
			
			for ( var i:int = startIndex; i < endIndex; i ++ )
			{
				( _listData.data[ i ].item as BaseItem ).playHideAnimate( i, startIndex );
			}
		}
		
		/**
		 * 跳过显示动画 
		 * 
		 */		
		public function skipShowAnimation():void
		{
			var len:int = _listData.length;
			
			titleGroup.alpha = 1;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				( _listData.data[ i ].item as BaseItem ).skipShowAnimate();
			}
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			disposeItems();
			Starling.juggler.removeTweens( titleGroup );
		
			list.dispose();
			list = null;
			firstTitle.dispose();
			firstTitle = null;
			disposeOther();
			titleGroup.dispose();
			titleGroup = null;
			super.dispose();
		}
		
		protected function disposeOther():void
		{
			
		}
		
		public function disposeLabels( arr:Array ):void
		{
			var len:int = arr.length;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				this[ arr[ i ] + "Title" ].dispose();
				this[ arr[ i ] + "Title" ] = null;
			}
		}
		
		/**
		 * 析构列表renderer 
		 * 
		 */		
		public function disposeItems():void
		{
			if ( _listData )
			{
				for ( var i:int = 0; i < _listData.length; i ++ )
				{
					if ( _listData.data[ i ].itemData )
					{
						_listData.data[ i ].itemData = null;
					}
					
					_listData.data[ i ].item.dispose();
					_listData.data[ i ].item = null;
				}
				
				_listData.removeAll();
				_listData = null;
			}
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function BaseListView()
		{
			super();
		}
	}
}